package com.cea.papyrus.diagram.statemachine.helper;

import org.eclipse.uml2.uml.BehavioredClassifier;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.State;
import org.eclipse.uml2.uml.Transition;
import org.eclipse.uml2.uml.Trigger;
import org.eclipse.uml2.uml.UMLFactory;

import com.cea.papyrus.core.helper.NodeHelper;
import com.cea.papyrus.core.utils.Debug;
import com.cea.papyrus.diagraminterchange2.di2.GraphElement;

public class NodeTriggerHelper extends NodeHelper {

	/**
	 * The channel.
	 */
	private int channel = 501;

	/**
	 * The default constructor.
	 */
	public NodeTriggerHelper(){
		super();
		Debug.debug(this,"Call NodeTriggerHelper()", channel);
	}

	@Override
	public boolean canCreateUMLElement(Element umlParent){
		Debug.debug(this,"Call boolean NodeTriggerHelper.canCreateUMLElement(Element umlParent)", channel);
		if((umlParent instanceof BehavioredClassifier) || (umlParent instanceof Transition) || (umlParent instanceof State))
			return true;
		return false;
	}
	
	@Override
	public boolean canCreateGraphElement(GraphElement parent) {
		return false;
	}

	@Override
	public Element createUMLElement(String name, Element umlParent) {
		Debug.debug(this,"Call Element NodeTriggerHelper.createUMLElement(String name, Element umlParent)", channel); //$NON-NLS-1$
		Trigger aTrigger = UMLFactory.eINSTANCE.createTrigger();
		aTrigger.setName(name);
		if(umlParent != null){
			if(umlParent instanceof BehavioredClassifier)
				((BehavioredClassifier)umlParent).getOwnedTriggers().add(aTrigger);
			else if(umlParent instanceof Transition)
				((Transition)umlParent).getTriggers().add(aTrigger);
			else if(umlParent instanceof State)
				((State)umlParent).getDeferrableTriggers().add(aTrigger);
		}
		return aTrigger;
	}

	@Override
	public boolean addUMLElement(Element element, Element umlParent){
		Debug.debug(this,"Call Element NodeTriggerHelper.addUMLElement(element element, Element umlParent)", channel); //$NON-NLS-1$
		if(umlParent != null){
			if(umlParent instanceof BehavioredClassifier)
				return ((BehavioredClassifier)umlParent).getOwnedTriggers().add((Trigger)element);
			else if(umlParent instanceof Transition)
				((Transition)umlParent).getTriggers().add((Trigger)element);
			else if(umlParent instanceof State)
				((State)umlParent).getDeferrableTriggers().add((Trigger)element);
		}
		return false;
	}
	
	@Override
	public boolean removeUMLElement(Element element, Element umlParent) {
		Debug.debug(this,"Call Element NodeTriggerHelper.removeUMLElement(element element, Element umlParent)", channel); //$NON-NLS-1$
		if(umlParent != null){
			if(umlParent instanceof BehavioredClassifier)
				return ((BehavioredClassifier)umlParent).getOwnedTriggers().remove(element);
			else if(umlParent instanceof Transition)
				((Transition)umlParent).getTriggers().remove(element);
			else if(umlParent instanceof State)
				((State)umlParent).getDeferrableTriggers().remove(element);
		}
		return false;
	}

	@Override
	public String getBaseString() {
		return "Trigger_";
	}
	
	@Override
	public String getNewUMLElementName(Element umlParent){
		String name = ""; //$NON-NLS-1$

		//i <10000: avoid infinite loops
		for(int i=0; i< 10001; i++ ) {
			name = getBaseString()+i;
			if(umlParent == null)
				return name;
			if(umlParent instanceof BehavioredClassifier){
				if(((BehavioredClassifier)umlParent).getOwnedTrigger(name) == null)
					return name;
			}
			else if(umlParent instanceof Transition){
				if(((Transition)umlParent).getTrigger(name) == null)
					return name;
			}
			else if(umlParent instanceof State){
				if(((State)umlParent).getDeferrableTrigger(name) == null)
					return name;
			}
			else
				return name;
		}
		return getBaseString()+"X"; //$NON-NLS-1$
	}

}
